<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuimini/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="addMenu"> 新建</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="articleTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="access">访问</a>
            <a class="layui-btn layui-btn-warm layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs data-count-edit" lay-event="top">置顶</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'miniTab'], function () {
        let $ = layui.jquery,
            miniTab = layui.miniTab,
            form = layui.form,
            table = layui.table;

        miniTab.listen();

        table.render({
            elem: '#articleTableId',
            url: '/admin/api/menu',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'id', width: 50, title: 'ID'},
                {field: 'text', minWidth: 100, title: '标题'},
                {field: 'icon', width: 100, title: '图标'},
                {field: 'url', width: 100, title: '跳转'},
                {field: 'ordinal', width: 80, title: '顺序'},
                {field: 'createdAt', width: 200, title: '发布', sort: true},
                {title: '操作', width: 230, toolbar: '#currentTableBar', align: "center"}
            ]],
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('articleTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'addArticle') {  // 监听添加操作
                miniTab.openNewTabByIframe({
                    href: "/admin/newArticle",
                    title: "新建文章"
                });
            }

            if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('articleTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }

        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }
            if (obj.event === 'access') {
                window.open('/blog/' + data.id)
            }
            if (obj.event === 'edit') {
                // 打开新的窗口
                miniTab.openNewTabByIframe({
                    href: "/admin/newArticle?articleId=" + data.id,
                    title: "编辑文章:" + data.id,
                });
            }
        });

    });
</script>

</body>
</html>